package com.android.tutu;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.xmlpull.v1.XmlPullParser;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.text.format.Time;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {
  TextView lastupdate, currentDate;
  AutoCompleteTextView debugbox;
  Spinner displist, destlist;
  ListView mainlist;
  SharedPreferences settings;
  ArrayList<String> Times = new ArrayList<String>();
  ArrayAdapter<String> listadapter;

  public void SM(String s) {
    Toast.makeText(this, s, 1000).show();
  }

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    currentDate = (TextView) findViewById(R.id.MainCurrentDate);
    lastupdate = (TextView) findViewById(R.id.MainLastUpdate);
    displist = (Spinner) findViewById(R.id.MainDispList);
    destlist = (Spinner) findViewById(R.id.MainDestList);
    // debugbox = (AutoCompleteTextView) findViewById(R.id.MainDebugBox);

    mainlist = (ListView) findViewById(R.id.MainList);
    // ArrayAdapter<String> listadapter = new ArrayAdapter<String>(this,
    // android.R.layout.simple_list_item_1, names);

    settings = getSharedPreferences(getResources().getString(R.string.Preferences_file), 0);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Shedule.StationNames);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    displist.setAdapter(adapter);
    destlist.setAdapter(adapter);
    displist.setOnItemSelectedListener(new OnItemSelectedListener() {
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Shedule.Disp = Shedule.Stations[position];
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("Dispatch", Shedule.Disp);
        editor.commit();
      }

      public void onNothingSelected(AdapterView<?> arg0) {
      }
    });
    destlist.setOnItemSelectedListener(new OnItemSelectedListener() {
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Shedule.Dest = Shedule.Stations[position];
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("Destination", Shedule.Dest);
        editor.commit();
      }

      public void onNothingSelected(AdapterView<?> arg0) {
      }
    });

    Shedule.Init(getApplicationContext());
    Shedule.MakeShedule();

    String LastUpdate = settings.getString("LastUpdate", "Never");
    if(LastUpdate != "Never") {
      Shedule.LastUpdate.parse(LastUpdate);
      SM("Loading...\n- LastUpdate");
      LastUpdate = Shedule.LastUpdate.format("%d-%m-%Y   %H:%M");
    }
    String Place = settings.getString("Dispatch", "?");
    if(Place != "?") {
      Shedule.Disp = Place;
      displist.setSelection(Shedule.ConvertIndex(Shedule.Disp));

      Place = settings.getString("Destination", "?");
      Shedule.Dest = Place;
      destlist.setSelection(Shedule.ConvertIndex(Shedule.Dest));

      SM("Loading...\n- Dispatch\n- Destination");
    }

    Time tm = new Time();
    tm.setToNow();
    currentDate.setText(tm.format("%d-%m-%Y"));
    lastupdate.setText(LastUpdate);
  }

  public void onClickMain(View v) {
    switch(v.getId()) {
    case R.id.MainUpdateButton:
      String ans = Shedule.Update();
      if(ans == "WIN") {
        SM("Successfuly updated");
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("LastUpdate", Shedule.LastUpdate.format2445());
        editor.commit();
        lastupdate.setText(Shedule.LastUpdate.format("%d-%m-%Y   %H:%M"));
      } else {
        SM("Some problems with:\n" + ans);
      }
      break;
    case R.id.MainDebugButton:
      Shedule.MakeShedule();
      SM(Shedule.MakeList());

      Times.clear();
      Iterator<Time> s = Shedule.Times.iterator();
      while(s.hasNext()) {
        Time t = s.next();
        Times.add(t.format("%d-%m-%Y   %H:%M"));
      }
      SM(Integer.toString(Times.size()));
      String[] test = new String[Times.size()];
      for(int i=0; i<Times.size(); i++)
        test[i] = Times.get(i);
      listadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, test);
      mainlist.setAdapter(listadapter);

      break;
    case R.id.MainExchangeButton:
      String buf = Shedule.Disp;
      Shedule.Disp = Shedule.Dest;
      Shedule.Dest = buf;
      displist.setSelection(Shedule.ConvertIndex(Shedule.Disp));
      destlist.setSelection(Shedule.ConvertIndex(Shedule.Dest));
      break;
    }
  }
}